package com.ykht.proj.activity.timer;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.activity.Enum.AgreementStatusEnum;
import com.ykht.proj.activity.Enum.CardStatusEnum;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.constant.RedisConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.Enum.PlanStatusEnum;
import com.ykht.proj.activity.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Component
@SuppressWarnings("all")
@Slf4j
@EnableScheduling
public class ActivityTimer {

	@Resource
	private VBrandAgreementService vBrandAgreementService;
	@Resource
	private VDiscountPlanService vDiscountPlanService;
	@Resource
	private VActivityService vActivityService;
	@Resource
	private VInfoService vInfoService;
	@Resource
	private VCardService vCardService;
	@Resource
	private RedisTemplate redisTemplate;
	/**
	 * 新活动发送订阅消息
	 */
	@SneakyThrows
	@Scheduled(cron = "0 0 8 * * ?")
	public void  activityMsg(){
		log.info("->定时任务：活动发送订阅");
		Set<Long> set = redisTemplate.opsForSet().members(RedisConstant.ACTIVITY_ID);
		log.info("缓存里的活动Id:{}",set);
		List<VActivity> list = vActivityService.list(Wrappers.lambdaQuery(VActivity.class).between(VActivity::getStartTime,LocalDateTime.now().plusDays(-1),LocalDateTime.now()).notIn(set.size()>0,VActivity::getId,set));
		for (VActivity o:list) {
			vActivityService.sendMq(o);
			redisTemplate.opsForSet().remove(RedisConstant.ACTIVITY_ID,o.getId());
		}
	}


	/**
	 * 协议定时任务
	 * @Scheduled(cron = "0 0/3 * * * ? ")  每3分钟
	 * @Scheduled(cron = "0 1 0 1/1 * ? ")  凌晨0点1分
	 */
	@Scheduled(cron = "0 1 0 1/1 * ? ")
	public void  agreementExpireTask(){
		log.info("->定时任务：协议 自动过期");
		UpdateWrapper<VBrandAgreement> endUpdateWrapper = new UpdateWrapper();
		endUpdateWrapper.lambda().lt(VBrandAgreement::getEndTime, LocalDateTime.now())
				.eq(VBrandAgreement::getAgreementStatus, AgreementStatusEnum.AT_WORK.getValue())
				.set(VBrandAgreement::getAgreementStatus, AgreementStatusEnum.BE_OVERDUE.getValue());
		vBrandAgreementService.update(endUpdateWrapper);
		log.info("->定时任务：协议 自动开始");
		UpdateWrapper<VBrandAgreement> staUpdateWrapper = new UpdateWrapper();
		staUpdateWrapper.lambda().lt(VBrandAgreement::getStaTime, LocalDateTime.now())
				.ge(VBrandAgreement::getEndTime, LocalDateTime.now())
				.eq(VBrandAgreement::getAgreementStatus, AgreementStatusEnum.TO_START.getValue())
				.set(VBrandAgreement::getAgreementStatus, AgreementStatusEnum.AT_WORK.getValue());
		vBrandAgreementService.update(staUpdateWrapper);
	}


	/**
	 * 优惠方案定时任务
	 * @Scheduled(cron = "0 0/3 * * * ? ")  每3分钟
	 * @Scheduled(cron = "0 1 0 1/1 * ? ")  凌晨0点1分
	 */
	@Scheduled(cron = "0 1 0 1/1 * ? ")
	public void  discountPlanExpireTask(){
		log.info("->定时任务：优惠方案 自动过期");
		UpdateWrapper<VDiscountPlan> endUpdateWrapper = new UpdateWrapper();
		endUpdateWrapper.lambda().lt(VDiscountPlan::getPlanEndTime, LocalDateTime.now())
				.eq(VDiscountPlan::getPlanStatus, PlanStatusEnum.AT_WORK.getValue())
				.set(VDiscountPlan::getPlanStatus, PlanStatusEnum.BE_OVERDUE.getValue());
		vDiscountPlanService.update(endUpdateWrapper);
		log.info("->定时任务：优惠方案 自动开始");
		UpdateWrapper<VDiscountPlan> staUpdateWrapper = new UpdateWrapper();
		staUpdateWrapper.lambda().lt(VDiscountPlan::getPlanStaTime, LocalDateTime.now())
				.ge(VDiscountPlan::getPlanEndTime, LocalDateTime.now())
				.eq(VDiscountPlan::getPlanStatus, PlanStatusEnum.TO_START.getValue())
				.set(VDiscountPlan::getPlanStatus, PlanStatusEnum.AT_WORK.getValue());
		vDiscountPlanService.update(staUpdateWrapper);
	}

	/**
	 * 卡（商品） 定时任务
	 * @Scheduled(cron = "0 0/3 * * * ? ")  每3分钟
	 * @Scheduled(cron = "0 1 0 1/1 * ? ")  凌晨0点1分
	 */
	@Scheduled(cron = "0 1 0 1/1 * ? ")
	public void  cardExpireTask(){
		log.info("->定时任务：卡（商品） 自动过期");
		UpdateWrapper<VCard> endUpdateWrapper = new UpdateWrapper();
		endUpdateWrapper.lambda().lt(VCard::getSaleEndTime, LocalDateTime.now())
				.eq(VCard::getCardStatus, CardStatusEnum.AT_WORK.getValue())
				.set(VCard::getCardStatus, PlanStatusEnum.BE_OVERDUE.getValue());
		vCardService.update(endUpdateWrapper);
		log.info("->定时任务：卡（商品） 自动开始");
		UpdateWrapper<VCard> staUpdateWrapper = new UpdateWrapper();
		staUpdateWrapper.lambda().lt(VCard::getSaleStaTime, LocalDateTime.now())
				.ge(VCard::getSaleEndTime, LocalDateTime.now())
				.eq(VCard::getCardStatus, CardStatusEnum.TO_START.getValue())
				.set(VCard::getCardStatus, CardStatusEnum.AT_WORK.getValue());
		vCardService.update(staUpdateWrapper);
	}


	/**
	 * 会员卡定时任务
	 * @Scheduled(cron = "0 0/3 * * * ? ")  每3分钟
	 * @Scheduled(cron = "0 1 0 1/1 * ? ")  凌晨0点1分
	 */
	@Scheduled(cron = "0 1 0 1/1 * ? ")
	public void  infoExpireTask(){
		log.info("->定时任务：会员卡 自动过期");
		UpdateWrapper<VInfo> endUpdateWrapper = new UpdateWrapper();
		endUpdateWrapper.lambda().lt(VInfo::getEndTime, LocalDateTime.now())
				.eq(VInfo::getStatus, CardConstant.INFO_STATUS_WDH)
				.set(VInfo::getStatus, CardConstant.INFO_STATUS_YDH);
		vInfoService.update(endUpdateWrapper);
	}


	/**
	 * 活动定时任务
	 * @Scheduled(cron = "0 0/3 * * * ? ")  每3分钟
	 * @Scheduled(cron = "0 1 0 1/1 * ? ")  凌晨0点1分
	 */
	@Scheduled(cron = "0 1 0 1/1 * ? ")
	public void  activityExpireTask(){
		vActivityService.activityExpireTask();
	}




}
